/*******************************************************************************
 * Copyright (c) 2012 Daniele Parisi.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Daniele Parisi
 *     neb.inf@gmail.com
 *     http://www.danieleparisi.com/
 ******************************************************************************/
package org.jxreport.html.chart;

import org.jxreport.core.Table;
import org.jxreport.core.views.chart.line.Line;
import org.jxreport.core.views.chart.line.LineChartView;
import org.jxreport.core.views.chart.line.MarkerOptions;
import org.jxreport.core.views.chart.line.Point;
import org.jxreport.html.HTMLView;
import org.w3c.dom.Element;

public class HTMLLineChartView extends LineChartView implements HTMLView {

	public HTMLLineChartView(Element chartElement, Table table) {
		super(chartElement, table);
	}

	@Override
	public String getJQueryCode()
	{
		String code = getInputJSCode() + " " +
				"var " + super.id + " = $.jqplot('" + super.chartId + "', " + this.getInputArrayJSCode() + ", {" +
						"title:'" + super.title + "', " + 
					    "legend: {" + 
					        "show: " + this.isShowLegend() + ", " +
					        "location: 'ne', " +	// compass direction, nw, n, ne, e, se, s, sw, w.
					        "xoffset: 12, " +		// pixel offset of the legend box from the x (or x2) axis.
					        "yoffset: 12, " + 		// pixel offset of the legend box from the y (or y2) axis.
				    	"}, " +
						"series: " + this.getSeriesJSCode() + 
		    	"});";
		
		return code;
	}

	@Override
	public String getInnerDivCode() {
		return "<div id=\""+ super.chartId + "\"></div>";
		//return "";
	}
	
	protected String getInputJSCode() {
		
		String code = "";
		int i=0;
		for (Line line : this.lines) {
			code += "var line" + i + " = []; ";
			for (Point point : line.getPoints())
				code += "line" + i + ".push([" + point.getX() + ", " + point.getY() + "]); ";
			i++;
		}
		
		return code;
	}
	
	protected String getInputArrayJSCode() {
		String code = "[";
		
		boolean first = true;
		int i=0;
		for (Line line : this.lines) {
			if(first) first = false;
			else code += ", ";
			
			code += "line" + i;
			i++;
		}
		
		code += "]";
		return code;
	}
	
	protected String getSeriesJSCode() {
		String code = "[";
		
		boolean first = true;
		for (Line line : this.lines) {
			if(first) first = false;
			else code += ", ";
			
			code += "{" + 
						"label: '" + line.getLabel() +  "', " +
						"color: '" + line.getColor() +  "', " +
						"lineWidth:" + line.getLineWidth() +  ", " +
						"showLine:" + line.getShowLine() +  ", " +
						"showMarker:" + line.getShowMarker() +  ", " +
						"markerOptions: { " + 
							MarkerOptions.COLOR + ": '" + line.getMarkerOptions().getColor()+ "', " +
							MarkerOptions.LINE_WIDTH + ": '" + line.getMarkerOptions().getLineWidth()+ "', " +
							MarkerOptions.SHADOW + ": '" + line.getMarkerOptions().getShadow() + "', " +
							MarkerOptions.SHADOW_ALPHA + ": '" + line.getMarkerOptions().getShadowAlpha() + "', " +
							MarkerOptions.SHADOW_ANGLE + ": '" + line.getMarkerOptions().getShadowAngle() + "', " +
							MarkerOptions.SHADOW_DEPTH + ": '" + line.getMarkerOptions().getShadowDepth() + "', " +
							MarkerOptions.SHADOW_OFFSET + ": '" + line.getMarkerOptions().getShadowOffset() + "', " +
							MarkerOptions.SHADOW + ": '" + line.getMarkerOptions().getShow() + "', " +
							MarkerOptions.SIZE + ": '" + line.getMarkerOptions().getSize() + "', " +
							MarkerOptions.STYLE + ": '" + line.getMarkerOptions().getStyle() + "'" +
						"}" + 
		          	"}";
		}
		
		code += "]";
		return code;		
	}
}
